import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from "@nestjs/common";

// 传入需要catch的类型
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
    catch(exception: HttpException, host: ArgumentsHost) {
        // 上下文对象
        const ctx = host.switchToHttp()

        // 响应对象
        const response = ctx.getResponse()
        // 请求对象
        const request = ctx.getRequest()
        // 状态码
        const status = exception.getStatus()

        // 测试 方式 输入一段不存在的接口 ： /user/get-all/asdas
        response.status(status).json({
            code:status,
            timeStamp:new Date().toISOString(),
            path:request.path,
            method:request.method,
            message:exception.message || exception.name
        })

    }
}